package com.ssi.report.common.utils;

/** 
 * 
 * 	Developed By 	: Kroekpong Sakulchai
 *  Date 			: 09-MAY-2014
 *  Version 		: Alpha
 *  Team			: Software Solution And Integration
 *   
 * **/

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class SSDateUtil {
	
	public static final Locale LOCALE_EN = Locale.ENGLISH;
	public static final Locale LOCALE_TH = new Locale("th", "TH");
	
	public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
	
	private static final DateFormat DATETIME_FORMAT_DEFAULT = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD , LOCALE_EN);
	
	public static String dateToString(Object date) {
		if (date != null && date instanceof Date) {
			return DATETIME_FORMAT_DEFAULT.format(date);
		} else {
			return null;
		}
	}
	
	public static String dateToString(Object date, Locale locale) {
		if (date != null && date instanceof Date) {
			return  new SimpleDateFormat(DATE_FORMAT_YYYYMMDD, locale).format(date);
		} else {
			return null;
		}
	}
	
	public static String dateToString(Object date, String format) {
		if (date != null && date instanceof Date) {
			return new SimpleDateFormat(format, LOCALE_EN).format(date);
		} else {
			return null;
		}
	}
	
	public static String dateToString(Object date, String format, Locale locale) {
		if (date != null && date instanceof Date) {
			return new SimpleDateFormat(format, locale).format(date);
		} else {
			return null;
		}
	}
	
	public static Date stringToDate(String dateStr) {
		Date date = null;
		if (dateStr == null || dateStr.length() == 0) {
			return date;
		}
		
		try {
			date = DATETIME_FORMAT_DEFAULT.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date stringToDate(String dateStr, Locale locale) {
		Date date = null;
		if (dateStr == null || dateStr.length() == 0) {
			return date;
		}
		
		try {
			date = new SimpleDateFormat(DATE_FORMAT_YYYYMMDD, locale).parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date stringToDate(String dateStr, String format) {
		Date date = null;
		if (dateStr == null || dateStr.length() == 0) {
			return date;
		}
		
		try {
			date = new SimpleDateFormat(format, LOCALE_EN).parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	public static Date stringToDate(String dateStr, String format, Locale locale) {
		Date date = null;
		if (dateStr == null || dateStr.length() == 0) {
			return date;
		}
		
		try {
			date = new SimpleDateFormat(format, locale).parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	/*public static void main(String[] args) {
		String sysdate = "25570425"; 
		Date today = new Date();
		System.out.println(SSDateUtil.stringToDate(sysdate));
		System.out.println(SSDateUtil.stringToDate(sysdate, LOCALE_TH));
		System.out.println(SSDateUtil.dateToString(today));
		System.out.println(SSDateUtil.dateToString(today, LOCALE_TH));
		System.out.println(SSDateUtil.dateToString(today, "dd/MM/yy hh:mm:ss "));
		System.out.println(SSDateUtil.dateToString(today, "dd/MM/yy hh:mm:ss ", LOCALE_TH));
	}*/
	
}
